package com.onairm.baselibrary.tv;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * Created by apple on 17/5/25.
 *
 * 处理 RecyclerView  的焦点错乱问题
 */

public class RecyclerLayoutManagers extends LinearLayoutManager {
    private  static final String TAG="LinLayoutManagers";
    private int orientationState=VERTICAL;//默认垂直方向
    private boolean tag = false;
    public RecyclerLayoutManagers(Context context) {
        super(context);
    }

    public RecyclerLayoutManagers(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
    }

    public RecyclerLayoutManagers(Context context, int orientation, boolean reverseLayout, boolean tag) {
        super(context, orientation, reverseLayout);
        this.tag = tag;
    }

    public RecyclerLayoutManagers(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public View onFocusSearchFailed(View focused, int focusDirection, RecyclerView.Recycler recycler, RecyclerView.State state) {
        return super.onFocusSearchFailed(focused, focusDirection, recycler, state);
    }

    @Override
    public void setOrientation(int orientation) {
        super.setOrientation(orientation);
        orientationState=orientation;
    }

    @Override
    public View onInterceptFocusSearch(View focused, int direction) {
      //  return super.onInterceptFocusSearch(focused, direction);
        Log.i(TAG,"onInterceptFocusSearch , focused = "+focused +"  direction = "+direction);
       // int span = getSpanCount();
        int count = getItemCount();
        int fromPos = getPosition(focused);
        if(orientationState==HORIZONTAL){
            switch (direction) {
                //case View.FOCUS_UP:
                case View.FOCUS_LEFT:
                    //fromPos = (fromPos - span);
                    fromPos--;
                    break;
                case View.FOCUS_DOWN:
                    if (tag) {
                        return super.onInterceptFocusSearch(focused, direction);
                    }
                case View.FOCUS_RIGHT:
                    //fromPos = (fromPos + span);
                    fromPos++;
                    break;
            }
        }else if(orientationState==VERTICAL){
            switch (direction) {
                //case View.FOCUS_UP:
                case View.FOCUS_UP:
                    //fromPos = (fromPos - span);
                    fromPos--;
                    break;
                //case View.FOCUS_DOWN:
                case View.FOCUS_DOWN:
                    //fromPos = (fromPos + span);
                    fromPos++;
                    break;
            }
        }

        Log.i(TAG, "onInterceptFocusSearch , fromPos = "+fromPos + " count = " + count);
        if(fromPos < 0) {
            return focused;
        } else if(fromPos >= count) {
            return focused;
        } else {
            return findViewByPosition(fromPos);
        }
    }

    @Override
    public int findFirstVisibleItemPosition() {
        return super.findFirstVisibleItemPosition();
    }
}
